# -*- coding:utf-8 -*-
import os

import django
from django import shortcuts
from django import http
import gdata.photos, gdata.photos.service

# Change to your Picasa Login and Password
username = ''
pwd = ''

def ShowAlbums(request):
    if username == '' or pwd == '':
        return http.HttpResponse(content="Can't connect to Picasa. <br />Put Username and Password in picasa_image.py" )
    gd_client = gdata.photos.service.PhotosService()
    gd_client.ClientLogin(username, pwd)
    albums = gd_client.GetUserFeed().entry
    msg = ''
    tmplImage = """
        <div style="float: left;
                    margin: 10px;
                    overflow: hidden;
                    text-align: center;
                    width: 100px;">
            <a href="javascript:EventFieldsDialog.insert('<img src=%s />')">
                <img src="%s" />
            </a>
            <br />
            %s
            <br />
            <a href="/picasa_image/del_photo/%s/%s">Delete photo</a>
        </div>
    """
    
    for album in albums:
        msg +=  '<br style="clear: left;" />Title: %s, number of photos: %s<br />' % (album.title.text,
                                            album.numphotos.text)
        photos = gd_client.GetFeed('/data/feed/api/user/%s/albumid/%s?kind=photo&imgmax=1600' % (
                                                    username, album.gphoto_id.text))
        for photo in photos.entry:
            msg +=  tmplImage % (
                                    photo.media.content[0].url,
                                    photo.media.thumbnail[0].url, photo.title.text
                                    ,album.gphoto_id.text,photo.gphoto_id.text)
    return http.HttpResponse(content=msg)

def AddPhoto(request):
    if request.FILES:
        gd_client = gdata.photos.service.PhotosService()
        gd_client.ClientLogin(username, pwd)
        albums = gd_client.GetUserFeed().entry
        filename =request.FILES['userfile'] 
        photo = gd_client.InsertPhotoSimple('/data/feed/api/user/%s/albumid/%s' % (
                                                    username, albums[0].gphoto_id.text)
                                                    , request.FILES['userfile'].name 
                                                    , request.FILES['userfile'].name
                                                    , filename
                                                    , content_type=request.FILES['userfile'].content_type)    
    
    return http.HttpResponseRedirect('/static/js/weditor/plugins/picasa_image/dialog.htm')
    
def DelPhoto(request, album_id, photo_id): 
    
    gd_client = gdata.photos.service.PhotosService()
    gd_client.ClientLogin(username, pwd)
    p = gd_client.GetFeed('/data/feed/api/user/%s/albumid/%s?kind=photo' % (username, album_id)).entry
    z = filter(lambda x: x.gphoto_id.text == photo_id, p)
    if len(z) >0:
        gd_client.Delete(z[0])

    return http.HttpResponseRedirect('/static/js/weditor/plugins/picasa_image/dialog.htm')
